package GUI;
/**
 * Configure class Screen for the administrator
 */

import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;

import Common.StudentClass;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ConfigureClass extends JPanel {
	private JLabel lblConfigureClass = null;
	private JLabel lblSelectClass = null;
	private JLabel lblSelectStudent = null;
	private JComboBox cBSelClass = null;
	private JComboBox cBSelStudent = null;
	private JButton btnAdd = null;

	private Login myLoginFrame = null;
	private UserAdmin user = null;
	public AdminMainMenu amn = null;
	private Vector<String> unAttStudents;
	private String selClass = null;
	private String selStudent = null;
	
	
	
	
	
	
	private JComboBox getcBSelClass(Vector<String> classes)
	{
		if(cBSelClass==null)
		{
		 cBSelClass = new JComboBox(classes);
		 cBSelClass.addActionListener((new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					selClass=cBSelClass.getSelectedItem().toString();
			 		System.out.println(selClass);
					
					
				}
			}));
		cBSelClass.setBounds(140, 77, 133, 20);
			
		}
		
		return cBSelClass;
	}
	
	private JComboBox getcBSelStudent(Vector<StudentClass> unattributedStud)
	{
		unAttStudents = new Vector<String>();
		
		for(int i=0;i<unattributedStud.size();i++)
		{
			
			System.out.println("Test Alon"+unattributedStud.get(i).FirstName.toString() +" "+ 
					unattributedStud.get(i).LastName.toString() +" "+ unattributedStud.get(i).Sid.toString());
			unAttStudents.add(unattributedStud.get(i).FirstName.toString() +" "+ 
		unattributedStud.get(i).LastName.toString() +" "+ unattributedStud.get(i).Sid.toString() );
		}
		
		
		if(cBSelStudent==null)
		{
		cBSelStudent = new JComboBox(unAttStudents);
		cBSelStudent.addActionListener((new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selStudent=cBSelStudent.getSelectedItem().toString();
		 		System.out.println(selStudent);
				
				
			}
		}));
		cBSelStudent.setBounds(140, 150, 133, 20);
		}
		
		return cBSelStudent;
	}
	
	private JButton getbtnAdd(final AdminMainMenu amn)
	{
		if(btnAdd==null)
		{

		 btnAdd = new JButton("Add");
		 btnAdd.addActionListener(new ActionListener() {
		 	public void actionPerformed(ActionEvent arg0) {
		 		amn.user.dataAttSt.setSelClass(selClass);
		 		amn.user.dataAttSt.setSelStudent(selStudent);
		 		String[] tmp=amn.user.dataAttSt.getSelClass().split(" ");
		 		String ClassId=tmp[tmp.length-1];
		 		tmp=amn.user.dataAttSt.getSelStudent().split(" ");
		 		String Sid=tmp[tmp.length-1];
		 		
		 		amn.user.dataAttSt.UpdateExecuted=false;
		 		amn.myLoginFrame.client.handleMessageFromClientUI("InsertStudentToClass "+ClassId+" "+Sid);
		 		while(!amn.user.dataAttSt.UpdateExecuted);
		 	

		 	}
		 });
		btnAdd.setBounds(403, 254, 89, 23);
			
		}
		return btnAdd;
	}
	
	private void initialize(AdminMainMenu AdminMenu)
	{
	
		setLayout(null);
		
		lblConfigureClass = new JLabel("Configure Class");
		lblConfigureClass.setBounds(242, 11, 116, 14);
		add(lblConfigureClass);
		
		lblSelectClass = new JLabel("Select class:");
		lblSelectClass.setBounds(32, 80, 98, 14);
		add(lblSelectClass);
		
		lblSelectStudent = new JLabel("Select student:");
		lblSelectStudent.setBounds(32, 153, 73, 14);
		add(lblSelectStudent);
		
		
		add(getcBSelClass(AdminMenu.user.dataAttSt.Classes));
		
		add(getcBSelStudent(AdminMenu.user.dataAttSt.UnattributedStud));
		
		add(getbtnAdd(AdminMenu));
		
	}
	
	
	
	
	
	public ConfigureClass(AdminMainMenu AdminMenu) {
		
		super();
		initialize(AdminMenu);
		
		
		
	
	}
}
